package com.canvas.controller;

import com.canvas.pojo.Users;
import com.canvas.pojo.bo.UsersBO;
import com.canvas.pojo.vo.UsersVO;
import com.canvas.service.UserService;
import com.canvas.utils.FastDFSClient;
import com.canvas.utils.FileUtils;
import com.canvas.utils.IMoocJSONResult;
import com.canvas.utils.MD5Utils;
import com.google.common.base.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: 宗恣
 * @Date: 2018/10/16 20:08
 */
@RestController
@RequestMapping("/u/")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FastDFSClient fastDFSClient;

    @PostMapping("registOrLogin")
    public IMoocJSONResult registOrLogin(@RequestBody Users users) throws Exception {

        // 1.判断用户名和密码不能为空
        if (Strings.isNullOrEmpty(users.getUsername()) ||
                Strings.isNullOrEmpty(users.getPassword())) {
            return IMoocJSONResult.errorMsg("用户名或密码不能为空");
        }

        // 2.判断用户名是否存在，如果存在就登录，反之则注册
        boolean usernameIsExist = userService.queryUsernameIsExist(users.getUsername());

        Users result = null;
        if (usernameIsExist) {
            // 2.1 登录
            result = userService.queryUserForLogin(users.getUsername(),
                            MD5Utils.getMD5Str(users.getPassword()));

            if (null == result) {
                return IMoocJSONResult.errorMsg("用户名或密码不正确...");
            }
        } else {
            // 2.2 注册
            result = userService.saveUsers(users);
        }

        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(result, usersVO);

        return IMoocJSONResult.ok(usersVO);
    }

    /**
     * 上传用户头像
     */
    @PostMapping("uploadFaceBase64")
    public IMoocJSONResult uploadFaceBase64(@RequestBody UsersBO userBO) throws Exception {

        // 获取前段传过来的base64字符串, 然后转换为文件对象再上传
        String base64Data = userBO.getFaceData();
        String userFacePath = "E:\\" + userBO.getUserId() + "userface64.png";
        FileUtils.base64ToFile(userFacePath, base64Data);

        // 上传文件到fastdfs
        MultipartFile faceFile = FileUtils.fileToMultipart(userFacePath);
        String faceUrl = fastDFSClient.uploadBase64(faceFile);
        System.out.println(faceUrl);

        // 获取缩略图的url
        String thump = "_80x80.";
        String[] arr = faceUrl.split("\\.");
        String thumpImgUrl = arr[0] + thump + arr[1];

        Users users = new Users();
        users.setId(userBO.getUserId());
        users.setFaceImage(thumpImgUrl);
        users.setFaceImageBig(faceUrl);

        Users result = userService.updateUserInfo(users);

        return IMoocJSONResult.ok(result);
    }

}
